
layui.define(function(exports){
	var $ = layui.$
	,setter = layui.setter
	,admin = layui.admin
	,carousel = layui.carousel;

	function inietChart() {
		if (sessionStorage.category == 'ORG') {
			$('.LAY_chart_month').hide();
			return;
		}

		admin.req({
            url: setter.url + '/product/categoryStatisticsMonth'
            ,type: 'GET'
            ,done: function (res) {
              var data = res.data || []
              ,months = data.map(function (item) {
                return item.month;
              })
              ,prices = data.map(function (item) {
                return item.price || 0;
              });
      
              var echartsApp = []
              ,options = [
                {
                  title: {
                    text: '年度销售额统计',
                    x: 'center',
                    textStyle: {
                      fontSize: 14
                    }
                  },
                  legend: {
                    data:['','']
                  },
                  xAxis : [{
                    type : 'category',
                    data: months
                  }],
                  yAxis : [{
                    type : 'value',
                    axisLabel:{formatter:'{value}'}
                  }],
                  series : [{
                    name:'销售额',
                    type:'line',
                    data: prices,
                    itemStyle: {        
                      normal: {
                          label: {
                              formatter:'{c}',
                              show: true,
                              position: 'top',
                              textStyle: {
                                  color: '#333',
                                  fontSize: 14
                              }
                          }
                      }
                    }
                  }]
                }
              ]
              ,view = $('#LAY-month-dataview').children('div')
              ,renderView = function(index){
                echartsApp[index] = echarts.init(view[index], layui.echartsTheme);
                echartsApp[index].setOption(options[index]);
                //window.onresize = echartsApp[index].resize;
                admin.resize(function(){
                    echartsApp[index].resize();
                });
              };
      
              var index = 0;
      
              if (view[0]) {
                renderView(0);
              
                //监听数据概览轮播
                carousel.on('change(LAY-month-dataview)', function(obj){
                  renderView(index = obj.index);
                });
              }
      
              //监听侧边伸缩
              layui.admin.on('side', function(){
                setTimeout(function(){
                  if(view[0]) {
                    renderView(index);
                  }
                }, 300);
              });
              
              //监听路由
              layui.admin.on('hash(tab)', function(){
                if(view[0]) {
                  layui.router().path.join('') || renderView(index);
                }
              });
            }
          });
	}

	exports('monthChart', {
		init: inietChart
	})
});